package com.xsmart.oa.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.QueryAdminManageDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 管理员管理界面
 * @author lxh
 * @date 2017/11/22
 */
@Service
public class QueryAdminService {

    @Autowired
    QueryAdminManageDAO queryAdminManageDAO;


    /**
     * 查询所有管理员对应的信息
     * @param requestBean
     * @return
     */
    public ResponseBean queryAdminList(RequestBean requestBean) {
        String userName = requestBean.getRequestParamString("userName");
        Integer roleId = requestBean.getRequestParamInteger("roleId");
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        ResponseBean responseBean = requestBean.getResponseBean();
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        List<Map<String,Object>> adminList=queryAdminManageDAO.queryAdmin(page, userName, roleId);
        //根据权限编码查询到对应的权限说明
        for(int i=0;i<adminList.size();i++){
            Map<String,Object>adminListMap=adminList.get(i);
            String actionList=(String)adminListMap.get("action_list");
            List<String> actionListDesc=new ArrayList<String>();
            if(actionList!=null){
                String[]array=actionList.split(",");
                actionListDesc= queryAdminManageDAO.queryActionDesc(array);
            }
            StringBuffer sb=new StringBuffer();
            for(int j=0;j<actionListDesc.size();j++){
                sb.append(actionListDesc.get(j)).append(',');
            }
            String actionDesc =sb.toString();
            if(actionDesc.length()>0){
                actionDesc=actionDesc.substring(0,actionDesc.length()-1);
            }
            adminListMap.put("action_list",actionDesc);
            adminList.set(i,adminListMap);
        }
        page.setRecords(adminList);
        responseBean.addResultPageItem("adminInfo", page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 删除对应管理员
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean deleteAdmin(RequestBean requestBean) {
        Integer userId = requestBean.getRequestParamInteger("userId");
        ResponseBean responseBean = requestBean.getResponseBean();
        try {
            int count = queryAdminManageDAO.deleteAdminById(userId);
            responseBean.setResult(ResultConstant.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }
        return responseBean;
    }

    /**
     * 更新对应管理员信息
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean updateAdmin(RequestBean requestBean) {
        Integer userId = requestBean.getRequestParamInteger("userId");
        String userName = requestBean.getRequestParamString("userName");
        String email = requestBean.getRequestParamString("email");
        String password = requestBean.getRequestParamString("password");
        String addTime = requestBean.getRequestParamString("add_time");
        String lastLogin = requestBean.getRequestParamString("last_login");
        String actionList = requestBean.getRequestParamString("action_list");
        String navList = requestBean.getRequestParamString("nav_list");
        Integer roleId = requestBean.getRequestParamInteger("roleId");


        Map<String, Object> adminMap = new HashMap<String, Object>();
        adminMap.put("userId",userId);
        adminMap.put("userName",userName.trim());
        adminMap.put("email",email);
//        adminMap.put("password",password.trim());
        adminMap.put("addTime",addTime);
//        adminMap.put("lastLogin",lastLogin);
        adminMap.put("actionList",actionList);
//        adminMap.put("navList",navList);
        adminMap.put("roleId",roleId);
        ResponseBean responseBean = requestBean.getResponseBean();
        try {
            int count = queryAdminManageDAO.updateAdminById(adminMap);
            responseBean.setResult(ResultConstant.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }
        return responseBean;
    }

    /**
     * 添加管理员
     * @param requestBean
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean saveAdmin(RequestBean requestBean) {

        Integer userId = requestBean.getRequestParamInteger("userId");
        String userName = requestBean.getRequestParamString("userName");
        String email = requestBean.getRequestParamString("email");
        String password = requestBean.getRequestParamString("password");
        Integer roleId = requestBean.getRequestParamInteger("roleId");
        Map<String, Object> adminMap = new HashMap<String, Object>();

        adminMap.put("userId", userId);
        adminMap.put("userName", userName.trim());
        adminMap.put("email", email);
        adminMap.put("password", password.trim());
        adminMap.put("roleId", roleId);
        ResponseBean responseBean = requestBean.getResponseBean();
        try {
            int count = queryAdminManageDAO.saveAdmin(adminMap);
            responseBean.setResult(ResultConstant.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseBean.setResult(ResultConstant.ERROR);
            throw new RuntimeException();
        }

        return responseBean;
    }
}
